define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			needs: ["businessDetail", "businessDetailNodedetail"],

			app: Ember.computed.alias('controllers.businessDetail.model'),

			node: Ember.computed.alias('controllers.businessDetailNodedetail.model'),

			pinTimePoint: Ember.computed.alias('controllers.businessDetail.pinTimePoint'),

			clickTimePoint: Ember.computed.oneWay('controllers.businessDetail.clickTimePoint'),

			initTime: function(){
		        var ctrl = this;
		        var startDate = ctrl.get("startDate");
		        var endDate = ctrl.get("endDate");
		        if(this.get("clickTimePoint") != null ){
		        	var date = new Date(this.get("clickTimePoint"));
		        	ctrl.set("endDate",new Date(+date+60000));
		        	if((isNaN(startDate))){
		        		ctrl.set("startDate",new Date(+date+60000)-60000);
		        	}else{
		        		ctrl.set("startDate",new Date(+date+60000)-(Math.abs(+endDate-(+startDate))));
		        	}
		        	
		        	
		        	setTimeout(function(){
		        		var startDate = APM.util.formatDate(new Date(+ctrl.get("startDate")), "yyyy/MM/dd H:mm");
		        		var endDate = APM.util.formatDate(new Date(+ctrl.get("endDate")), "yyyy/MM/dd H:mm");
		        		$('#config-demo').val(startDate+' - '+endDate);
		        	});
		        	
		        }else{
		        	var sdate = new Date(new Date() - timeD_value - 60000-60000);
			        sdate.setSeconds(0);
			        sdate.setMilliseconds(0); 
			        ctrl.set("startDate",new Date(+sdate));
			        var edate = new Date(new Date() - timeD_value-60000);
			        edate.setSeconds(0);
			        edate.setMilliseconds(0); 
			        ctrl.set("endDate",new Date(+edate));
		        }
		    },
			/*timePointLisenterEvent:Ember.computed("pinTimePoint", function(key, value){
				var view = Broada.getView("businessTimeRangesTopo");
				if(this.get("pinTimePoint") != null){
					var date = new Date(this.get("pinTimePoint"));
					if(typeof view != "undefined"){
						var range = view.getTimeRange().endDate - view.getTimeRange().startDate;	
						if(range == 60000){
							this.set("startDate", new Date(date.getTime()-range+60000));
							this.set("endDate", new Date(date.getTime()+60000));
						}else{
							this.set("startDate",new Date(date.getTime()-range+60000));
							this.set("endDate", new Date(date.getTime()+60000));
						}
					}
					typeof view != "undefined"?view.set("btnTxt", date?APM.util.formatDate(date, "f"):null):null;
					this.requireDataList1();
				}
			}),*/

			gridColumns: Ember.A(),

			gridData: Ember.A(),

			pageIndex: 0,

			pageSize: 10,

			menuIdChange: Ember.observer("model.id", function(){
				var ctrl = this,
					menuId = this.get("model.id");

				this.set("pageIndex", 0);
				this.set("gridColumns", Ember.A());
				this.set('keyWord', '');
				this.requireColumns(menuId)
				.then(function(columns){
					ctrl.set("gridColumns", columns);
					return ctrl.requireDataList1();
				});
			}),

			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							ctrl.requireData();
						};
					});
				},0)
			},

			requireColumns: function(menuId){
				var ctrl = this;
				var params = {
					menuId: menuId
				};
				if( isLocalDebugModel ){
					params = undefined;
				}
				return this.store.ajax("GET", "node/menus/columns", params)
				.then(function(response){
					var columns = Ember.A();
					var allowSort = false;
					Ember.A(response.result).forEach(function(item){
						var cloumn = {
							headerText: item.name,
							dataKey: item.dataKey,
							width: item.width,
							allowSort: allowSort,
							ensurePxWidth: !!item.width,
							textAlignment: item.align,
							_action: item.action,
							_fmtType: item.transform,
							cellFormatter: function(args, colume){
								if (args.row.type & $.wijmo.wijgrid.rowType.data) {
									var $container = args.$container,
										val = args.row.data[colume.dataKey];
									if(colume._action){
										$container.addClass("btn-link");
									}
									if(colume._fmtType){
										$container.html( APM.util.indicatorFormatter(colume._fmtType)(val) );
										return true;
									}
									// if(String(val).length > 60){
										var $item = $('<div class="column-summary"></div>').appendTo($container);
										$item.html(APM.util.escape(val));
										$container.attr("title",val);
										return true;
									// }
								}
							}
						};
						allowSort = true;
						columns.push(cloumn);
					});
					return columns;
				});
			},

			requireDataList: function(args){
				var ctrl = this;

				var params = {
					appId: this.get("app.id"),
					nodeId: this.get("node.id")
				};
				Ember.merge(params, args);
				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}
				return this.store.ajax("GET", "node/menus/data", params);
			},

			requireDataList1: function(){
				var ctrl = this;

				var params = {
					menuId: this.get("model.id"),
					keyWord: this.get("keyWord"),
					startDate: +this.get("startDate"),
					endDate: +this.get("endDate"),
					pageSize: ctrl.get("pageSize"),
					pageIndex: this.get("pageIndex"),
					orderBy: this.get("orderBy"),
					desc: this.get("desc")
				};

				//开发调试模式参数
				if( isLocalDebugModel ){
					params = {};
				}

				this.requireDataList(params)
				.then(function(response){
					ctrl.set("gridData", Ember.A(response.result));
					ctrl.set("totalRows", typeof response.meta.total=="undefined"?-1:response.meta.total);
					if (response.result.length == 0) {
						ctrl.set("pageIndex",0);
						ctrl.set("pageSize",10);
						ctrl.set("totalRows",0);
					}else{

					}
				});
			},

			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							ctrl.requireDataList1();
						};
					});
				},0)
			},

			actions: {
				gridCellClicked: function(e, args, view){
					var ctrl = this,
						row = args.cell.row(),
						column = args.cell.column();
					if (row && row.type & $.wijmo.wijgrid.rowType.data) {
						if(column._action && column._action.name == "showDetail"){
							this.send(column._action.name, row.data, column.dataKey, column._action.args);
							Broada.getView("businessDetailNodedetailDynamicDialog").openDialog();
						}
					}
				},

				pageIndexChanged: function(e, data, view){
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);

					this.requireDataList1();
				},

				scvDownload: function(){
					var ctrl = this,
						nodeId = this.get('node.id'),
						appId = this.get('app.id'),
						menuId = this.get("model.id"),
						startDate = +this.get("startDate"),
						endDate = +ctrl.get("endDate"),
						orderBy = this.get("orderBy"),
						desc = this.get("desc")||'',
						keyword = this.get("keyword")||'';

					var url="/front/rest/apm/node/menus/downloadCVSData?appId="+appId+"&menuId="+menuId+"&desc="+desc+"&nodeId="+nodeId+"&keyWord="+keyword+"&startDate="+startDate+"&endDate="+endDate+"&token="+encodeURIComponent($.cookie("token"));

					if(orderBy){
						url = url+"&orderBy="+orderBy;
					}
	
					confirmDialog("确定要导出报表吗？")
					.then(function(){
						window.location.href =url;
					});
				},

				gridSorted: function(e, data, view){
					view.ensureControl(false);
					this.set("orderBy", data.column.dataKey);
					this.set("desc", data.sortDirection=="descending"||undefined);
					this.requireDataList1();
				},

				gridSorted2: function(e, data, view){
					view.ensureControl(false);
					this.set("orderBy", data.column.dataKey);
					this.set("desc", data.sortDirection=="descending"||undefined);
					this.requireDataList2();
				},

				search: function() {
					this.set("pageIndex", 0);
					this.requireDataList1();
				},

				timePointChange: function(timeRange){
					this.set("startDate",timeRange.startDate);
					this.set("endDate",timeRange.endDate);
			
					this.requireDataList1();
				},

				timeRangesChange: function(timeRange){

					var ctrl = this;
					var pinTimePoint = ctrl.get("controllers.businessDetail").pinTimePoint;

						ctrl.set("startDate", timeRange.startDate);
						ctrl.set("endDate", timeRange.endDate);
			
					
					var view = Broada.getView("businessTimeRangesTopo");
					if(ctrl.get("pinTimePoint") != timeRange.startDate){
						ctrl.set("pinTimePoint", null);
					}

					/*if(timeRange.selectVal == "n1M"||timeRange.selectVal == "n1d"){
						ctrl.set("startDate", timeRange.startDate);
						ctrl.set("endDate", timeRange.endDate);
					};*/

					ctrl.requireDataList1();






					// var ctrl = this,
					// 	locationStart = this.get("locationStart");
					// var isSelectedTime = ctrl.get("controllers.businessDetail").isSelectedTime;
					

					// if(!isSelectedTime){
					// 	ctrl.set("startDate", timeRange.startDate);
					// 	ctrl.set("endDate", timeRange.endDate);
					// }else{
					// 	var view = Broada.getView("businessTimeRangesTopo");
					// 	if(this.get("pinTimePoint") != null){
					// 		this.set("locationStart",true);
					// 		var date = new Date(this.get("pinTimePoint"));
					// 		if(typeof view != "undefined"){
					// 			var range = view.getTimeRange().endDate - view.getTimeRange().startDate;
					// 			if(range == 60000){
					// 				this.set("startDate", new Date(date.getTime()-range+60000));
					// 				this.set("endDate", new Date(date.getTime()+60000));
					// 			}else{
					// 				this.set("startDate",new Date(date.getTime()-range+60000));
					// 				this.set("endDate", new Date(date.getTime()+60000));
					// 			}
					// 		}
					// 		if(ctrl.get("pinTimePoint") != (+view.get("selectedDate"))){
					// 			ctrl.set("pinTimePoint", null);
					// 		}
					// 		this.requireDataList1();
					// 	}
					// };

					// if(timeRange.selectVal == "n1M"||timeRange.selectVal == "n1d"){
					// 	ctrl.set("startDate", timeRange.startDate);
					// 	ctrl.set("endDate", timeRange.endDate);
					// };

					if(this.get("gridColumns.length") > 0){
						this.set("pageIndex", 0);
						this.requireDataList1();
					}
				}
			}
		});

		Controller.reopen({
			gridColumns2: Ember.A(),

			gridData2: Ember.A(),

			pageIndex2: 0,

			pageSize2: 10,

			requireDataList2: function(){
				var ctrl = this,
					args = this.get("dataList2Args");

				var params = {
					startDate: +this.get("startDate"),
					endDate: +this.get("endDate"),
					pageIndex: this.get("pageIndex2"),
					orderBy: this.get("orderBy"),
					desc: this.get("desc")
				};
				Ember.A(args).forEach(function(arg){
					params[arg.key] = arg.value;
				});
				delete params.next_menu_id;

				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}
				
				this.requireDataList(params)
				.then(function(response){
					ctrl.set("gridData2", Ember.A(response.result));
					ctrl.set("totalRows2", response.meta.total);
				});
			},

			actions: {
				pageIndexChanged2: function(e, data, view){
					this.set("pageIndex2", data.newPageIndex);

					this.requireDataList2();
				},

				showDetail: function(data, dataKey, args){
					var ctrl = this,
						menuId = getValueFromArgs(args, "next_menu_id");

					this.set("dataList2Args", [
						{key: "menuId", value: menuId},
						{key: "dataId", value: data.id},
						{key: "dataKey", value: dataKey}
					].concat(args));
					this.set("pageIndex2", 0);
					this.set("gridColumns2", Ember.A());

					this.requireColumns(menuId)
					.then(function(columns){
						ctrl.set("gridColumns2", columns);
						ctrl.requireDataList2();
					});
				}
			}
		});

		function getValueFromArgs(args, key){
			var arg = Ember.A(args).findBy("key", key);
			return arg&&arg.value;
		}

		APM.BusinessDetailNodedetailDynamicController = Controller;

		return Controller;
	}
);